home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #43 (Apr 89) / Designer CDEF Code / myCDEF.pas < prev   
Pascal/Delphi Source File  |  1988-12-30  |  8KB  |  264 lines

  1. UNIT myCDEF;
  2.  
  3. INTERFACE
  4.  
  5.     FUNCTION DesignerButtonProc (varCode : integer;
  6.                                     theControl : ControlHandle;
  7.                                     message : integer;
  8.                                     param : longint) : longint;
  9.  
  10. IMPLEMENTATION
  11.  
  12.     FUNCTION DesignerButtonProc; {}
  13. {Custom controls for our controls}
  14.         CONST
  15.             inactive = 255;
  16.             indicator = 254;
  17.             regbutton = 3;
  18.             regcheck = 4;
  19.             rStop = 5;
  20.             rCaution = 6;
  21.             rGo = 7;
  22.  
  23.         PROCEDURE DodrawCntl (theButton : integer;
  24.                                         ControlHdl : ControlHandle;
  25.                                         thePart : longint);
  26. {draws our controls}
  27.             VAR
  28.                 cRect, titleRect, designRect, tempRect, dimRect, onBox, offBox : Rect;
  29.                 height, width, curve, start, titlewidth : integer;
  30.                 titlestr, tempstr : str255;
  31.  
  32.             PROCEDURE DeactiveButton (theRect : rect);
  33.     {dim the button}
  34.             BEGIN
  35.                 PenPat(Gray);
  36.                 PenMode(PatBic);
  37.                 PaintRect(theRect);
  38.                 PenNormal;
  39.             END;
  40.  
  41.         BEGIN {of main drawing routines}
  42.             IF ControlHdl^^.contrlVis <> 0 THEN    {visible?}
  43.                 BEGIN
  44.                     HLock(Handle(ControlHdl));
  45.                     cRect := ControlHdl^^.contrlRect; {get the rectangle of control}
  46.                     CASE ControlHdl^^.contrlRfCon OF {switch on button type}
  47.                         regbutton :  {regular button}
  48.                             BEGIN
  49.                                 PenSize(2, 2);
  50.                                 curve := (cRect.bottom - cRect.top) DIV 2;
  51.                                 IF ControlHdl^^.contrlHilite > 0 THEN {hilight button?}
  52.                                     PenPat(black)
  53.                                 ELSE
  54.                                     PenPat(dkgray);
  55.                                 FrameRoundRect(cRect, curve, curve); {draw button}
  56.                                 PenNormal;
  57.                                 IF ControlHdl^^.contrlHilite = inactive THEN {inactive?}
  58.                                     BEGIN
  59.                                         tempRect := cRect;
  60.                                         InsetRect(tempRect, 2, 2);
  61.                                         DeactiveButton(tempRect);
  62.                                     END;
  63.                             END;
  64.  
  65.                         regcheck : {checkbox}
  66.                             BEGIN
  67.                                 EraseRect(cRect);
  68.                 {could be better by just erasing what is needed}
  69.  
  70.                                 titleRect := cRect;
  71.                                 titleRect.top := titleRect.bottom - 12;
  72.                                 designRect := cRect;
  73.                                 designRect.bottom := designRect.bottom - 12;
  74.                                 width := titleRect.right - titleRect.left;
  75.                                 height := designRect.bottom - designRect.top;
  76.  
  77.                 {draw control title}
  78.                                 TextFont(systemFont);             {Set the font to draw in}
  79.                                 titlewidth := StringWidth(ControlHdl^^.contrlTitle);
  80.                                 titlestr := ControlHdl^^.contrlTitle;
  81.                                 TextBox(Pointer(ord(@titlestr) + 1), length(titlestr), titleRect, teJustLeft);
  82.  
  83.                 {draw ON/OFF, or LEFT/RIGHT, titles}
  84.                                 tempRect := designRect;
  85.                                 tempRect.left := tempRect.left + 20;
  86.                                 tempRect.bottom := tempRect.top + 12;
  87.                                 tempstr := 'LEFT';
  88.                                 TextBox(Pointer(ord(@tempstr) + 1), length(tempstr), tempRect, teJustRight);
  89.                                 tempRect.bottom := designRect.bottom;
  90.                                 tempRect.top := tempRect.bottom - 12;
  91.                                 tempstr := 'RIGHT';
  92.                                 TextBox(Pointer(ord(@tempstr) + 1), length(tempstr), tempRect, teJustRight);
  93.                                 TextFont(applFont);               {Set the default application font}
  94.  
  95.                 {start drawing switch}
  96.                                 tempRect := designRect;
  97.                                 tempRect.right := tempRect.left + 40;
  98.                                 WITH tempRect DO {draw Switch}
  99.                                     BEGIN
  100.                                         width := right - left;
  101.                                         height := bottom - top;
  102.                                         onBox.top := top + (height DIV 2) - 10;
  103.                                         onBox.bottom := onBox.top + 20;
  104.                                         onBox.left := left + (width DIV 2) - 10;
  105.                                         onBox.right := onBox.left + 20;
  106.  
  107.                                         IF ControlHdl^^.contrlHilite > 0 THEN {Hilite?}
  108.                                             PenSize(2, 2)
  109.                                         ELSE
  110.                                             PenSize(1, 1);
  111.                                         IF ControlHdl^^.contrlValue > 0 THEN
  112.                                             BEGIN {draw on}
  113.                                                 FrameArc(onBox, 0, -270);
  114.                                                 MoveTo(left + (width DIV 2), onBox.top);
  115.                                                 Line(20, -20);
  116.                                                 Line(10, 10);
  117.                                                 LineTo(onBox.right, top + (height DIV 2));
  118.                                             END
  119.                                         ELSE
  120.                                             BEGIN {draw off}
  121.                                                 FrameArc(onBox, 90, -270);
  122.                                                 MoveTo(onBox.right, top + (height DIV 2));
  123.                                                 Line(20, 20);
  124.                                                 Line(-10, 10);
  125.                                                 LineTo(left + (width DIV 2), onBox.bottom);
  126.                                             END;
  127.                                     END; { of with}
  128.                             END;
  129.  
  130.                         rStop, rCaution, rGo : {radio button}
  131.                             BEGIN
  132.                                 designRect := cRect;
  133.                                 titleRect := designRect;
  134.                                 titleRect.left := titleRect.left + 25;
  135.                                 titleRect.top := titleRect.top + 3;
  136.                                 designRect.right := designRect.left + 20;
  137.  
  138.                 {draw title of control}
  139.                                 TextFont(systemFont);             {Set the font to draw in}
  140.                                 titlestr := ControlHdl^^.contrlTitle;
  141.                                 TextBox(Pointer(ord(@titlestr) + 1), length(titlestr), titleRect, teJustLeft);
  142.                                 TextFont(applFont);               {Set the default application font}
  143.                                 height := designRect.bottom - designRect.top;
  144.  
  145.                 {switch on RefCon for radio button: stop, caution, and go}
  146.                 {this is for radio button specific stuff}
  147.                                 CASE ControlHdl^^.contrlRfCon OF
  148.                                     rStop : 
  149.                                         BEGIN
  150.                     {draw box}
  151.                                             PenSize(2, 2);
  152.                                             MoveTo(designRect.left, designRect.bottom);
  153.                                             LineTo(designRect.left, designRect.top);
  154.                                             LineTo(designRect.right, designRect.top);
  155.                                             LineTo(designRect.right, designRect.bottom);
  156.                                             PenNormal;
  157.                                             PenPat(dkgray); {light color}
  158.                                         END;
  159.                                     rCaution : 
  160.                                         BEGIN
  161.                     {draw box}
  162.                                             PenSize(2, 2);
  163.                                             MoveTo(designRect.left, designRect.bottom);
  164.                                             LineTo(designRect.left, designRect.top);
  165.                                             MoveTo(designRect.right, designRect.top);
  166.                                             LineTo(designRect.right, designRect.bottom);
  167.                                             PenNormal;
  168.                                             PenPat(ltgray); {light color}
  169.                                         END;
  170.                                     rGo : 
  171.                                         BEGIN
  172.                     {draw box}
  173.                                             PenSize(2, 2);
  174.                                             MoveTo(designRect.left, designRect.top);
  175.                                             LineTo(designRect.left, designRect.bottom);
  176.                                             LineTo(designRect.right, designRect.bottom);
  177.                                             LineTo(designRect.right, designRect.top);
  178.                                             PenNormal;
  179.                                             PenPat(gray); {light color}
  180.                                         END;
  181.  
  182.                                     OTHERWISE
  183.                                         ;
  184.                                 END; { of case }
  185.  
  186.                 {draw light}
  187.                                 onBox := designRect;
  188.                                 InsetRect(onBox, 4, 4);
  189.                                 onBox.left := onBox.left + 1;
  190.                                 onBox.right := onBox.right + 1;
  191.                                 IF ControlHdl^^.contrlValue = 0 THEN {override pattern if off}
  192.                                     PenPat(white);
  193.                                 PaintOval(onBox);
  194.                                 PenNormal;
  195.                                 FrameOval(onBox);
  196.  
  197.                                 IF ControlHdl^^.contrlHilite = inactive THEN {deactivate control}
  198.                                     BEGIN
  199.                                         tempRect := cRect;
  200.                                         DeactiveButton(tempRect);
  201.                                     END;
  202.                             END;
  203.                     END; {of button}
  204.  
  205.                     HUnLock(Handle(ControlHdl));
  206.                 END; {of a visible control}
  207.         END;
  208.  
  209.         FUNCTION DotestCntl (theButton : integer;
  210.                                         ControlHdl : ControlHandle;
  211.                                         thePoint : point) : longint;
  212. {Test what part of control}
  213.         BEGIN
  214.             DotestCntl := 0; {initial value}
  215.             IF ControlHdl^^.contrlHilite <> inactive THEN {active control?}
  216.                 BEGIN
  217.                     IF ControlHdl^^.contrlHilite <> indicator THEN {indicator?}
  218.                         BEGIN
  219.                             IF PtInRect(thePoint, ControlHdl^^.contrlRect) THEN {in control's rect}
  220.                                 BEGIN
  221.                                     CASE ControlHdl^^.contrlRfCon OF {switch on control type}
  222.                     {here is where you send a code back as to what type of control}
  223.                     {and what part of that control was pressed.  We only have controls}
  224.                     {with one part, so no further testing is needed}
  225.                                         regbutton : 
  226.                                             DotestCntl := inButton;
  227.                                         regcheck, rStop, rCaution, rGo : 
  228.                                             DotestCntl := inCheckBox;
  229.                                         OTHERWISE
  230.                                             ;
  231.                                     END;
  232.                                 END;
  233.                         END
  234.                     ELSE
  235.                         DotestCntl := indicator;
  236.                 END;
  237.         END;
  238.  
  239.         PROCEDURE DocalcCRgns (theButton : integer;
  240.                                         ControlHdl : ControlHandle;
  241.                                         RegionHdl : RgnHandle);
  242. {calculate region of control}
  243.         BEGIN
  244.     {easy as pie if rectangular}
  245.             RectRgn(RegionHdl, ControlHdl^^.contrlRect);
  246.         END;
  247.  
  248.     BEGIN {main proc routine}
  249.         DesignerButtonProc := 0; {initialize the result}
  250.  
  251.     {switch on what we are to do}
  252.         CASE message OF
  253.             drawCntl : 
  254.                 DodrawCntl(varCode, theControl, param); {draw}
  255.             testCntl : 
  256.                 DesignerButtonProc := DotestCntl(varCode, theControl, Point(param)); {test}
  257.             calcCRgns : 
  258.                 DocalcCRgns(varCode, theControl, RgnHandle(param)); {region}
  259.             OTHERWISE
  260.                 ;
  261.         END; {of case}
  262.     END;
  263.  
  264. END.